package cn.shop.merchant.controller.gametemplate;

import cn.dev33.satoken.stp.StpUtil;
import cn.shop.merchant.model.gameconfig.GameInfo;
import cn.shop.merchant.model.gameconfig.GamePartition;
import cn.shop.merchant.req.GameInfoQueryReq;
import cn.shop.merchant.service.GameInfoService;
import cn.shop.merchant.service.GamePartitionService;
import cn.shop.utils.ApiResult;
import cn.shop.utils.annotation.OperationLog;
import cn.shop.utils.enums.OperationType;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.shop.utils.constant.Constant.UID;

/**
 * 游戏名接口
 * @author : 李一涵
 * @date : Created in 2022-2-26 10:06
 * @description :
 */
@RestController
@RequestMapping("/game")
public class GameInfoController {

    @Resource
    private GameInfoService gameInfoService;
    @Resource
    private GamePartitionService gamePartitionService;

    /**
     * 新增游戏名
     *
     * @param info 游戏名信息
     * @return 返回结果
     */
    @PostMapping("")
    @OperationLog(desc = "新增游戏", type = OperationType.CREATE)
    public ApiResult<Map<String, Object>> save(@Validated @RequestBody GameInfo info) {
        return ApiResult.result(gameInfoService.save(info));
    }

    /**
     * 修改游戏名
     *
     * @param info 游戏名信息
     * @return 返回结果
     */
    @PutMapping("")
    @OperationLog(desc = "修改游戏", type = OperationType.UPDATE)
    public ApiResult<Map<String, Object>> update(@Validated @RequestBody GameInfo info) {
        GameInfo update = new GameInfo();
        update.setName(info.getName());
        update.setId(info.getId());
        update.setTemplateId(info.getTemplateId());
        update.setAliasId(info.getAliasId());
        boolean success = gameInfoService.updateById(update);
        if (success) {
            gamePartitionService.update(Wrappers.<GamePartition>lambdaUpdate()
                    .set(GamePartition::getTemplateId, info.getTemplateId())
                    .eq(GamePartition::getGameId, info.getId()));
        }
        return ApiResult.result(success);
    }

    @GetMapping("/all")
    public ApiResult<List<Map<String, Object>>> allList() {
        long uid = StpUtil.getSession().getLong(UID);
        List<Map<String, Object>> data = gameInfoService.allList(uid);
        return ApiResult.success(data);
    }

    /**
     * 获取游戏名信息
     *
     * @param id 编号
     * @return 返回结果
     */
    @GetMapping("/{id}")
    public ApiResult<Map<String, Object>> edit(@PathVariable long id) {
        GameInfo info = gameInfoService.getById(id);
        Map<String, Object> map = new HashMap<>(4);
        if (info != null) {
            map.put("id", info.getId());
            map.put("name", info.getName());
            map.put("templateId", info.getTemplateId());
            map.put("aliasId", info.getAliasId());
        }
        return ApiResult.success(map);
    }

    /**
     * 获取游戏名列表
     *
     * @param req 查询条件
     * @return 返回结果
     */
    @GetMapping("")
    public ApiResult<Map<String, Object>> list(@Validated GameInfoQueryReq req) {
        long uid = StpUtil.getSession().getLong(UID);
        Map<String, Object> data = gameInfoService.pageList(uid, req);
        return ApiResult.success(data);
    }

    /**
     * 删除游戏
     *
     * @param id 编号，多个用逗号分割
     * @return 返回结果
     */
    @DeleteMapping("/{id}")
    @OperationLog(desc = "删除游戏", type = OperationType.DELETE)
    public ApiResult<Map<String, Object>> delete(@PathVariable long id) {
        long uid = StpUtil.getSession().getLong(UID);
        long count = gamePartitionService.countByGameId(id);
        if (count > 0) {
            return ApiResult.fail("该游戏下还有为移除的游戏分区，请先移除该游戏下的游戏分区");
        }
        return ApiResult.result(gameInfoService.deleteByIds(id, uid));
    }
}
